/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Shuriken.Core.Ranking; 
using Shuriken.Core.Store; 

namespace Shuriken.Core.UI.StandardUI
{
    /// <summary>
    /// Basic display slot implementation 
    /// </summary>
    class BaseItemController : IUIDisplaySlot 
    {
        private MutableIndexSet m_HitMask;
        private string m_SearchKey;
        private SortedDisplayItemList m_DisplayItems;
        private int m_SelectedItemIndex;
        private bool m_Visible;
        protected InputItemView m_InputPanel;
        protected frmMain m_ParentForm;
        private ISearchable m_CurrentDisplayItem;

        /// <summary>
        /// Constructor 
        /// </summary>
        /// <param name="ParentForm">Parent form</param>
        /// <param name="UIControlPanel">UI Control for displaying</param>
        public BaseItemController(frmMain ParentForm, InputItemView UIControlPanel)
        {
            m_InputPanel = UIControlPanel;
            m_InputPanel.RegisterParentForm(ParentForm);
            m_ParentForm = ParentForm; 
        }

        public bool SelectedSlot
        {
            get
            {
                return m_InputPanel.ControlSelected;
            }
            set
            {
                m_InputPanel.ControlSelected = value; 
            }
        }

        #region IUIDisplaySlot Members

        /// <summary>
        /// Gets the hit mask.
        /// </summary>
        public Shuriken.Core.Ranking.MutableIndexSet HitMask
        {
            get { return m_HitMask; }
        }

        /// <summary>
        /// Gets or sets the search key 
        /// </summary>
        public string SearchKey
        {
            get
            {
                return m_SearchKey;
            }
            set
            {
                m_SearchKey = value;

                if (m_SearchKey.Length > 0)
                {
                    m_InputPanel.SearchTerm = m_SearchKey;
                }
            }
        }

        /// <summary>
        /// Gets the list of display items 
        /// </summary>
        public Shuriken.Core.Store.SortedDisplayItemList DisplayItems
        {
            get { return m_DisplayItems; }
        }

        /// <summary>
        /// Gets the list of initial display items 
        /// </summary>
        /// <remarks>
        /// Returns an empty list for now.
        /// </remarks>
        public virtual Shuriken.Core.Store.SortedDisplayItemList InitialDisplayItems
        {
            get { return new SortedDisplayItemList(); }
        }

        /// <summary>
        /// Gets the index of the selected item. 
        /// </summary>
        public int SelectedItemIndex
        {
            get { return m_SelectedItemIndex; }
            set 
            {
                ChangeSelectedItem(value);
            }
        }

        /// <summary>
        /// TAB possible on this slot.
        /// </summary>
        public bool IsTABPossible
        {
            get
            {
                return TABPossible();
            }
        }

        /// <summary>
        /// Decides if tab is possible from this UI slot. 
        /// </summary>
        /// <returns>true if the next TAB is possible</returns>
        protected virtual bool TABPossible()
        {
            return false;
        }

        /// <summary>
        /// Selected item is changed. 
        /// </summary>
        /// <param name="NewIndex">New selected item.</param>
        /// <remarks>
        /// This method is used to change the selected item within the Display slot. The method is also declared as virtual, so the classes instantiated can override this method.
        /// This was needed in order to be able to react and reload the next UI slot in the chain.
        /// The solution is not very elegant. It needs to be changed to event based. 
        /// </remarks>
        protected virtual void ChangeSelectedItem(int NewIndex)
        {
            m_SelectedItemIndex = NewIndex;
            m_CurrentDisplayItem = m_DisplayItems[m_SelectedItemIndex];

            m_InputPanel.ShowDisplayItem(m_CurrentDisplayItem, SearchKey, DisplayItems.Count);
        }

        /// <summary>
        /// Gets the selected display item. 
        /// </summary>
        public ISearchable SelectedDisplayItem
        {
            get 
            {
                return m_CurrentDisplayItem;
            }

            set
            {
                m_CurrentDisplayItem = value; 
            }
        }
        

        /// <summary>
        /// Gets or sets the visibility of the slot 
        /// </summary>
        public bool Visible
        {
            get
            {
               return m_Visible;
            }
            set
            {
                m_Visible = value;
            }
        }

        /// <summary>
        /// Handling the back key pressed event. 
        /// </summary>
        /// <remarks>
        /// Handling the back key pressed event. Erases the Displayed content from the slot 
        /// </remarks>
        public virtual bool BackKeyPressed()
        {
            m_SearchKey = string.Empty;
            m_HitMask = new Shuriken.Core.Ranking.MutableIndexSet();
            SelectedDisplayItem.HitMask = m_HitMask;
            m_SelectedItemIndex = -1;
            m_DisplayItems.Clear();

            m_InputPanel.ShowDisplayItem(SelectedDisplayItem, SearchKey, DisplayItems.Count);
            m_ParentForm.HideItemList();

            return true; 
        }

        /// <summary>
        /// Updates this slot with new data
        /// </summary>
        /// <param name="SearchKey">search key</param>
        /// <param name="CurrentIndex">currently selected item index</param>
        /// <param name="HitMask">hitmask for the search key</param>
        /// <param name="DisplayItems">displayed items</param>
        public void UpdateDisplaySlot(string SearchKey, int CurrentIndex, List<Shuriken.Core.Store.ISearchable> DisplayItems)
        {
            m_SearchKey = SearchKey;
            m_HitMask = HitMask;
            m_DisplayItems = (SortedDisplayItemList)DisplayItems;

            ChangeSelectedItem(CurrentIndex);

            // Change only the selection on the list if we are the selected slot.
            if (SelectedSlot)
            {
                if (m_ParentForm.IsItemListVisible())
                {
                    m_ParentForm.UpdateListWithItems(m_DisplayItems);
                }

                m_ParentForm.SelectItemOnItemList(CurrentIndex);
            }
        }

        /// <summary>
        /// Select the next item on the details list. 
        /// </summary>
        /// <returns></returns>
        public bool SelectNextItemFromDetails()
        {
            return m_ParentForm.SelectNextItemFromDetails(this);
        }

        /// <summary>
        /// Selects the previous item on the details list. 
        /// </summary>
        /// <returns></returns>
        public bool SelectPreviousItemFromDetails()
        {
            return m_ParentForm.SelectPreviousItemFromDetails(this);
        }

        /// <summary>
        /// Selects the current item from details
        /// </summary>
        /// <returns></returns>
        public bool SelectCurrentItemFromDetails()
        {
            return m_ParentForm.SelectCurrentItemFromDetails(this);
        }

        /// <summary>
        /// Tells if the Display slot in search mode  
        /// </summary>
        public bool IsInSearchMode
        {
            get
            {
                return true; 
            }
        }

        /// <summary>
        /// Gets the display text 
        /// </summary>
        public string DisplayText
        {
            get
            {
                return m_CurrentDisplayItem.Name; 
            }
        }

        /// <summary>
        /// Erasing search key in the slot. 
        /// </summary>
        public void EraseSearchKey()
        {
            SearchKey = string.Empty; 
        }

        #endregion
    }
}
